{% load staticfiles %}
<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>风控白名单</title>

    <link rel="stylesheet" type="text/css" href=" {% static 'themes/default/easyui.css' %}">
    <link rel="stylesheet" type="text/css" href=" {% static 'themes/icon.css' %}">
	<link rel="stylesheet" type="text/css" href=" {% static 'themes/portal.css' %}">
	<link rel="stylesheet" type="text/css" href=" {% static 'themes/default/devbase.css' %}">
	<link rel="stylesheet" type="text/css" href=" {% static 'themes/default/base.css' %}">

	<script type="text/javascript" src=" {% static 'scripts/jquery.min.js' %}"></script>
	<script type="text/javascript" src=" {% static 'scripts/jquery.easyui.min.js' %}"></script>
	<script type="text/javascript" src=" {% static 'scripts/jquery.portal.js' %}"></script>
	<script type="text/javascript" src=" {% static 'scripts/easyui-lang-zh_CN.js' %}"></script>
	<script type="text/javascript" src=" {% static 'scripts/json2.js' %}"></script>
	<script type="text/javascript" src=" {% static 'scripts/devbase.js' %}"></script>
</head>
<body class="easyui-layout">

	<div data-options="region:'center'" class="easyui-layout">

		<!-- 查询面板 -->
		<div class="easyui-panel"
		    style="width:100%;height:150px;padding:10px;"
			data-options="iconCls:'icon-search',closable:false,region:'north',title:'风控白名单'">
			<table>
				<tr>
					<td class="label">产品:</td>
					<td><select id="search_combo_prod_id" class="easyui-combobox" style="width:200px;" data-options="valueField:'product_id',textField:'product_name',disabled:false"></select></td>
				</tr>
                <tr>
                    <td class="label">投顾账号 :</td>                                                                                   <!-- 下拉框显示 改为 account_name -->
                    <td><select id="search_account_id" class="easyui-combobox" style="width:200px;"
                                data-options="valueField:'invest_account_id',textField:'invest_account_name',disabled:false,editable:true" />
                    </td>
                    <td class="label">证券代码:</td>
                    <td><select id="search_stock_code" name="formobj.stock_code"
                                class="easyui-combobox" style="width:200px;"
                                data-options="
											valueField: 'stock_code',
											textField: 'stock_nbcode',
											panelHeight: 240,
											formatter: formatItem,
											url:'<%=contextPath%>/tm/tmStockOrderDataAction!queryListJsonStockInfo.do',
									        method:'post'
											">
                    </select></td>

                    <td><a id="search_btn" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'">查询</a></td>
                    <td><a id="export_btn" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'">导出Excel</a></td>
                </tr>

			</table>
		</div>

		<!-- 查询表格 -->
		<div class="easyui-panel"  data-options="closable:false,region:'center',title:''">
		<table id="dg" class="easyui-datagrid"
			style="width:100%;height:100%;"
			data-options="rownumbers:true,singleSelect:true,
				toolbar:toolbar,
				pagination:true,
				pageSize:10,
				pageList:[10,20,50]">
			<thead>
				<tr>
				    <th data-options="field:'rule_id',width:80" style="display:none">ID</th>
				    <!-- 隐藏id -->
                    <th data-options="field:'invest_account_id',width:120" style="display:none">投顾id</th>
                    <!-- 新加字段显示名字 -->
                    <th data-options="field:'invest_account_name',width:120">投顾账号</th>
                    <th data-options="field:'stock_code',width:120">证券代码</th>

                    <th data-options="field:'_operate',width:80,align:'center',formatter:formatOper">操作</th>
				</tr>
			</thead>
		</table>
		</div>
	</div>

	<!-- 操作对话框 -->
	<div id="dlg_add" class="easyui-dialog" title="编辑"
		style="width:400px;height:450px;padding:10px;"
		data-options="
				iconCls: 'icon-save',
				toolbar: '',
				buttons: [{
					text:'保存',
					iconCls:'icon-ok',
					handler:function(){
					    submitForm();
					}
				},{
					text:'关闭',
					handler:function(){
					    closeForm();
					}
				}]
			">

		<div style="margin:20px 0;"></div>
		<div>
			<form id="ff" method="post">
				<table cellpadding="5">
                   <tr>
                        <td class="label">投顾账号:</td>                                                                                                       <!-- 下拉框显示 改为 account_name -->
                       <td><select id="account_id" name="formobj.account_id" class="easyui-combobox"
                                   style="width:200px;"
                                   data-options="valueField:'invest_account_id',textField:'invest_account_name',disabled:false,editable:true,
                        				required:true" /></td>
                    </tr>
                    <tr>
                        <td class="label">证券代码:</td>
                        <td><select id="stock_code" name="formobj.stock_code"
                                    class="easyui-combobox" style="width:200px;"
                                    data-options="
											valueField: 'stock_code',
											textField: 'stock_nbcode',
											panelHeight: 240,
											formatter: formatItem,
											url:'<%=contextPath%>/tm/tmStockOrderDataAction!queryListJsonStockInfo.do',
									        method:'post',
									        required:true
											"></select></td>
                    </tr>

				</table>
				<input type="hidden" name="formobj.rule_id" id="rule_id" />
			</form>
		</div>
	</div>


	<script type="text/javascript">
		// 股票代码 de 格式函数
		function formatItem(row){
			var s = '<span style="font-weight:bold">' + row.stock_nbcode + '</span>' +
					'<span style="color:#888"> ' + row.stock_name + '</span>';
			return s;
		}

		// 设置股票代码 选择的回调函数
		$('#search_stock_code').combobox({
			onSelect: function(rec){
				// 设置 股票名称框
				// console.debug("onSelect: " + rec.stock_name+',' + rec.stock_code);
			},
			// 内容 改变事件响应
			onChange : function(newValue, oldValue) {
				if (newValue.length <= 6) {
					getStockInfoData('search_stock_code', newValue.toUpperCase());
					// console.debug("onChange: " + newValue + ',' + oldValue);
				}
			},
			// 设置输入对下拉框载入数据完成处理
			onLoadSuccess: function(){
				// 如果数据列表只有一行，选择对应的选项
				var data = $('#search_stock_code').combobox('getData');
				if (data.length == 1){
					$('#search_stock_code').combobox('select', data[0].stock_code);
					// console.debug("onload: " + $('#search_stock_code').combobox('getValue') + " text " + $('#search_stock_code').combobox('getText'));
				}
				return false;
			},

		   // 设置输入对下拉框的过滤
		   filter: function(q, row){
				return false;
		   }
		});

		// 设置股票代码 选择的回调函数
		$('#stock_code').combobox({
			onSelect: function(rec){
				// 设置 股票名称框
				console.debug("onSelect: " + rec.stock_name+',' + rec.stock_code);
			},
			// 内容 改变事件响应
			onChange : function(newValue, oldValue) {
				if (newValue.length <= 6) {
					getStockInfoData('stock_code', newValue.toUpperCase());
					console.debug("onChange: " + newValue + ',' + oldValue);
				}
			},
			// 设置输入对下拉框载入数据完成处理
			onLoadSuccess: function(){
				// 如果数据列表只有一行，选择对应的选项
				var data = $('#stock_code').combobox('getData');
				if (data.length == 1){
					$('#stock_code').combobox('select', data[0].stock_code);
					console.debug("onload: " + $('#stock_code').combobox('getValue') + " text " + $('#stock_code').combobox('getText'));
				}
				return false;
			},

		   // 设置输入对下拉框的过滤
		   filter: function(q, row){
				return false;
		   }
		});

		// 获取股票列表数据
		function getStockInfoData(container, stock_code) {
			$('#' + container).combobox('reload',{
				search_stock_code : stock_code
			});
		}
	    //查询按钮操作
	    function queryData() {
           // alert('query');

            var cond = 'search_account_id=' + $('#search_account_id').combobox('getValue');
            cond += '&search_stock_code=' + $('#search_stock_code').combobox('getValue');
            cond += '&search_combo_prod_id=' + $('#search_combo_prod_id').combobox('getValue');
            console.log(cond);

            $.get('/rm/cmRiskWhite/query?'+cond, function (data) {
                console.log(data);
                obj = JSON.parse(data)
                $('#dg').datagrid('loadData', obj);
            });
		}

		$('#search_btn').bind('click', queryData);

		function combogrid(combo_id) {
			$('#'+combo_id).combogrid({
				disabled:false,
           		panelWidth: 500,
           		pagination:true,
           		pageSize:10,
           		pageList:[10,20,50],
				idField: 'stock_code',
				textField: 'stock_code',
				// url: '<%=contextPath%>/cm/cmStockInfoAction!queryListJson.do?page=1&rows=10000',
				url: '<%=contextPath%>/cm/cmStockInfoAction!queryListJson.do',
				method: 'post',
				columns: [[
					{field:'stock_code',title:'证券代码',width:80},
					{field:'stock_full_name',title:'证券全称',width:120},
					{field:'stock_name_en',title:'英文名称',width:80,align:'right'},
					{field:'stock_name',title:'证券名',width:80,align:'right'}
					// 证券下拉框去掉 两个字段
					// {field:'stock_type',title:'证券类型',width:80},
					// {field:'stock_detail_type',title:'类型详细',width:80,align:'center'}
				]],
				fitColumns: true
			});
		}

		/////
		var toolbar = [ {
			text : '新增',
			iconCls : 'icon-add',
			handler : function() {
				if ($('#search_combo_prod_id').combobox('getValue') == ""){
					alert("请选择一个产品");
				}
				else {
					//alert('add');
					$('#dlg_add').dialog('open');
				}
			}
		}, {
			text : '修改',
			iconCls : 'icon-edit',
			handler : function() {
				//alert('修改');
				var selected = getSelected();
				if (selected == "") {
					alert("请选择要修改的记录");
					return;
				} else {
					$('#dlg_add').dialog('open');
					loadFF(selected);
				}
			}
		}, {
			text : '删除',
			iconCls : 'icon-remove',
			handler : function() {
				var selected = getSelected();
				if (selected == "") {
					alert("请选择要删除的记录");
					return;
				} else {
					deleteData(selected);
				}
			}
		} ];

		function getSelected() {
			var selected = "";
			var row = $('#dg').datagrid('getSelected');
			if (row) {
				selected = row.rule_id;
			}
			return selected;
		}

		function submitForm() {
			if (!$("#ff").form('validate')) {
				alert('重新填写');
				return;
			}

			// 判断股票是否存在
			var codeIsTrue = false;
			var c = $('#stock_code').combobox('getValue');
			$.ajax({
				type : 'post',
				async : false,
				url:'<%=contextPath%>/tm/tmStockOrderDataAction!queryListJsonStockInfo.do',
				data : {
					search_stock_code : $('#stock_code').combobox('getValue').slice(2)
				},
				cache : false,
				dataType : 'json',
				success : function (data) {
					if (data.length != 1) {
						alert('股票代码不存在 ,重新填写');
					}else {
						codeIsTrue = true;
					}
				},
				error : function (x, s, e) {
					// alert('验证股票代码出错' + JSON.stringify(x) + JSON.stringify(s) + JSON.stringify(e));
					alert('验证股票代码出错');
				}
			});

			if (codeIsTrue) {
				var formParam = $("#ff").serialize();//序列化表格内容为字符串
				$.ajax({
					type : 'post',
					url : '<%=contextPath%>/rm/cmRiskWhiteRuleAction!saveData.do',
					data : formParam,
					cache : false,
					dataType : 'json',
					success : function(data) {
						if (data.code=='1') {
							//alert('操作成功');
							$('#dg').datagrid('reload');
							closeForm();
						}else{
							alert('操作失败' + data.msg);
						}
						// alert("操作成功");
						//window.location.reload();

					}
				});
			}
		}

		function loadFF(rule_id) {
			$.ajax({
				type : 'post',
				url : '<%=contextPath%>/rm/cmRiskWhiteRuleAction!loadData.do?rule_id=' + rule_id,
				data : {},
				cache : false,
				dataType : 'json',
				success : function(data) {
                    // 使用combobox
                    $("#account_id").combobox('setValue', data.account_id);
                    // 使用 combogrid
                    $("#stock_code").combobox('setValue', data.stock_code);

					$("#rule_id").val(data.rule_id);
				}
			});

		}

		function closeForm() {
			$('#ff').form('clear');
			$('#dlg_add').dialog('close');
		}

		function deleteData(rule_id) {
			var yz = $.ajax({
				type : 'post',
				url : '<%=contextPath%>/rm/cmRiskWhiteRuleAction!deleteData.do?rule_id=' + rule_id,
				data : {},
				cache : false,
				dataType : 'json',
				success : function(data) {
					if (data) {
						//alert("操作成功");
						//window.location.reload();
						$('#dg').datagrid('reload');
						closeForm();
					}
				},
				error : function() {
					alert("操作异常");
					return;
				}
			});
		}

		function formatOper(val, row, index) {
			return '<a href="#" onclick="editData(' + index + ')">修改</a>';

		}

		function editData(index) {
			$('#dg').datagrid('selectRow', index);// 关键在这里
			var row = $('#dg').datagrid('getSelected');
			if (row) {
				$('#dlg_add').dialog('open');
				loadFF(row.rule_id);
			}
		}

		////////
		$(function() {
			//界面统一设置
			// 隐藏account_id 2015-6-2, +1L
			$("#dg").datagrid('hideColumn', 'invest_account_id');
			$("#dg").datagrid('hideColumn', 'rule_id');
			$('#dlg_add').dialog('close');

			//扩展验证
			initValidate();

			//初始化界面设置
			$.get('/rm/cmRiskWhite/init', function(data) {
					//alert("getInitValue 操作成功");

					// 设置下拉框默认值
					try {
						console.log(data);
                        obj = JSON.parse(data)
                        obj[0]['selected'] = true
                        $('#search_combo_prod_id').combobox('loadData', obj);
						queryData();
						product_Relate_invest()
					} catch (e) {

					}
				}
			);

			// 设置产品下拉框和投顾账户联动
			$("#search_combo_prod_id").combobox(
				{
					editable : false,
					onSelect : product_Relate_invest
				});

			function product_Relate_invest(){
				$('#search_account_id').combobox('setValue', '');

				$.get('/rm/cmRiskWhite/getComboVal?search_type=2&search_combo=account_id&search_value='
							+ $('#search_combo_prod_id').combobox('getValue'), function(data) {
						$("#search_account_id").combobox(
							'loadData', data);
						$("#account_id").combobox(
							'loadData', data);
						queryData();
					}
				);
			}
		});
	</script>



</body>
</html>
